<?php

namespace App\Library\Logic;

use App\Library\Lock;
use App\Models\ZbBailModel;
use Illuminate\Database\Eloquent\Model;

class ZbBailLogic extends ZbBailModel
{
    /**
     * paySuccess
     * @param int $id
     * @param string $payAt
     * @return int
     */
    public static function paySuccess(int $id, string $payAt): int
    {
        return Lock::get(__CLASS__ . __FUNCTION__ . $id, function () use ($id, $payAt) {
            return self::query()->where(humpToLine(['id' => $id, 'statusPay' => 0, 'payAt' => null]))
                ->update(humpToLine(['payAt' => $payAt, 'statusPay' => 1]));
        });
    }

    /**
     * @param string $money
     * @return ?Model
     */
    public function last(string $money): ?Model
    {
        return self::query()->where('user_id', USERID)->where('pay_price', $money)
            ->where('order_price', $money)->orderBy('id', 'desc')->first();
    }

    /**
     * @param float $money
     * @return Model
     */
    public function add(float $money): Model
    {
        return self::insertForModel([
            'userId' => USERID,
            'pay_price' => $money,
            'order_price' => $money,
            'title' => '保证金:' . $money,
        ]);
    }

    /**
     * @return ?Model
     */
    public function current(): ?Model
    {
        return self::query()->where('user_id', USERID)->orderBy('id', 'desc')->first();
    }
}
